import * as THREE from 'three';
import startVertex from '../shader/2/vertex.glsl';
import startFragment from '../shader/2/fragment.glsl';
import endVertex from '../shader/3/vertex.glsl';
import endFragment from '../shader/3/fragment.glsl';

export default class Fireworks {
    constructor(color, position) {

        this.clock = new THREE.Clock();
        this.color = new THREE.Color(color);//转换为threejs的颜色，否则报错

        //设置烟花发射的点
        this.startGeometry = new THREE.BufferGeometry();
        this.startPositionArry = new Float32Array([0, 0, 0]);
        this.startGeometry.setAttribute('position', new THREE.BufferAttribute(this.startPositionArry, 3));

        //设置烟花飞行向量
        this.aStepArry = new Float32Array(3);
        this.aStepArry[0] = position.x - 0;
        this.aStepArry[1] = position.y - 0;
        this.aStepArry[2] = position.z - 0;
        this.startGeometry.setAttribute('aStep', new THREE.BufferAttribute(this.aStepArry, 3));

        this.startMaterial = new THREE.ShaderMaterial({
            vertexShader: startVertex,
            fragmentShader: startFragment,
            uniforms: {
                uTime: { value: 0 },
                uSize: { value: 0 },
                uColor: { value: this.color }
            },
            transparent: true,
            depthWrite: false,
            blending: THREE.AdditiveBlending
        });
        this.startPoint = new THREE.Points(this.startGeometry, this.startMaterial);

        const listener1 = new THREE.AudioListener();
        this.audio1 = new THREE.Audio(listener1);
        new THREE.AudioLoader().load('./assets/audio/send.mp3', (audioBuffer) => {
            this.audio1.setBuffer(audioBuffer);
            this.audio1.setLoop(false);//循环播放
            this.audio1.setVolume(1);
        });

        //设置爆炸烟花
        this.endGeometry = new THREE.BufferGeometry();
        this.fireworksCount = 200 + Math.floor(Math.random() * 200);
        this.endPositionArry = new Float32Array(this.fireworksCount * 3);
        this.endDirectionArry = new Float32Array(this.fireworksCount * 3);
        this.endScaleArry = new Float32Array(this.fireworksCount);
        for (let i = 0; i < this.fireworksCount; i++) {
            this.endPositionArry[i * 3] = position.x;
            this.endPositionArry[i * 3 + 1] = position.y;
            this.endPositionArry[i * 3 + 2] = position.z;

            let jiao1 = Math.random() * Math.PI * 2;
            let jiao2 = Math.random() * Math.PI * 2;
            let r = Math.random();
            this.endDirectionArry[i * 3] = r * Math.sin(jiao1) + r * Math.sin(jiao2);
            this.endDirectionArry[i * 3 + 1] = r * Math.sin(jiao1) + r * Math.cos(jiao2);
            this.endDirectionArry[i * 3 + 2] = r * Math.cos(jiao1) + r * Math.cos(jiao2);

            this.endScaleArry[i] = Math.random();
        };
        this.endGeometry.setAttribute('position', new THREE.BufferAttribute(this.endPositionArry, 3));
        this.endGeometry.setAttribute('direction', new THREE.BufferAttribute(this.endDirectionArry, 3));
        this.endGeometry.setAttribute('scale', new THREE.BufferAttribute(this.endScaleArry, 1));
        this.endMaterial = new THREE.ShaderMaterial({
            vertexShader: endVertex,
            fragmentShader: endFragment,
            uniforms: {
                uTime: { value: 0 },
                uSize: { value: 0 },
                uColor: { value: this.color }
            },
            blending: THREE.AdditiveBlending,
            transparent: true,
            depthWrite: false
        });
        this.endPoint = new THREE.Points(this.endGeometry, this.endMaterial);


        const listener2 = new THREE.AudioListener();
        this.audio2 = new THREE.Audio(listener1);
        new THREE.AudioLoader().load(`./assets/audio/pow${Math.floor(Math.random() * 4 + 1)}.ogg`, (audioBuffer) => {
            this.audio2.setBuffer(audioBuffer);
            this.audio2.setLoop(false);//循环播放
            this.audio2.setVolume(1);
        });


    }

    //添加至场景
    sceneAddobj(scene) {
        scene.add(this.startPoint);
        scene.add(this.endPoint);
        this.scene = scene;
    }


    update() {
        this.time = this.clock.getElapsedTime();
        if (this.time > 0.1 && this.time < 1) {
            this.startMaterial.uniforms.uTime.value = this.time;
            this.startMaterial.uniforms.uSize.value = 20;
            if (!this.audio1.isPlaying && !this.play1) {
                this.audio1.play();
                this.play1 = true;
            }
        } else if (this.time > 1) {
            const newTime = this.time - 1;
            this.startMaterial.uniforms.uSize.value = 0;

            this.scene.remove(this.startPoint);
            this.startPoint.clear();
            this.startGeometry.dispose();
            this.startMaterial.dispose();

            this.endMaterial.uniforms.uSize.value = 20;
            this.endMaterial.uniforms.uTime.value = newTime;

            if (!this.audio2.isPlaying && !this.play2) {
                this.audio2.play();
                this.play2 = true;
            };

            if (newTime > 5) {
                this.scene.remove(this.endPoint);
                this.endPoint.clear();
                this.endGeometry.dispose();
                this.endMaterial.dispose();
                return 'remove';
            }
        }
    }
}